﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using SEO.Business;

public partial class KeywordsSEOList : AdminPage
{
    /// <summary>
    /// 医院
    /// </summary>
    protected int MedicalID
    {
        get { return ViewState["MedicalID"] == null ? 0 : Convert.ToInt32(ViewState["MedicalID"]); }
        set { ViewState["MedicalID"] = value; }
    }

    /// <summary>
    /// 分类ID
    /// </summary>
    protected int SortID
    {
        get { return ViewState["SortID"] == null ? 0 : Convert.ToInt32(ViewState["SortID"]); }
        set { ViewState["SortID"] = value; }
    }

    /// <summary>
    /// 关键字
    /// </summary>
    protected string Keywords
    {
        get { return ViewState["Keywords"] == null ? "" : ViewState["Keywords"].ToString(); }
        set { ViewState["Keywords"] = value; }
    }

    /// <summary>
    /// 时间
    /// </summary>
    protected string BeginDateTime
    {
        get { return ViewState["BeginDateTime"] == null ? "" : ViewState["BeginDateTime"].ToString(); }
        set { ViewState["BeginDateTime"] = value; }
    }

    /// <summary>
    /// 状态
    /// </summary>
    protected bool Status
    {
        get { return ViewState["Status"] == null ? false : Convert.ToBoolean(ViewState["Status"].ToString()); }
        set { ViewState["Status"] = value; }
    }

    /// <summary>
    /// 次数
    /// </summary>
    protected int CountIndex
    {
        get { return ViewState["CountIndex"] == null ? 1 : Convert.ToInt32(ViewState["CountIndex"]); }
        set { ViewState["CountIndex"] = value; }
    }

    /// <summary>
    /// 收集时间
    /// </summary>
    protected string SysRunBeginDateTime
    {
        get { return ViewState["SysRunBeginDateTime"] == null ? "" : ViewState["SysRunBeginDateTime"].ToString(); }
        set { ViewState["SysRunBeginDateTime"] = value; }
    }

    /// <summary>
    /// 自动锁定
    /// </summary>
    protected int SysLock
    {
        get { return ViewState["SysLock"] == null ? -1 : Convert.ToInt32(ViewState["SysLock"]); }
        set { ViewState["SysLock"] = value; }
    }

    /// <summary>
    /// 手动锁定
    /// </summary>
    protected int DiyLock
    {
        get { return ViewState["DiyLock"] == null ? -1 : Convert.ToInt32(ViewState["DiyLock"]); }
        set { ViewState["DiyLock"] = value; }
    }

    /// <summary>
    /// 开始数
    /// </summary>
    protected int BeginCount
    {
        get { return ViewState["BeginCount"] == null ? 0 : Convert.ToInt32(ViewState["BeginCount"]); }
        set { ViewState["BeginCount"] = value; }
    }

    /// <summary>
    /// 结束数
    /// </summary>
    protected int EndCount
    {
        get { return ViewState["EndCount"] == null ? 10 : Convert.ToInt32(ViewState["EndCount"]); }
        set { ViewState["EndCount"] = value; }
    }
    protected int nMedicalID = 0;

    protected int nNum = 0;
    /// <summary>
    /// 关键字排名情况
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            BindMedical();
            InitSort();
            InitData();
        }
    }

    /// <summary>
    /// 权限控制
    /// </summary>
    /// <returns></returns>
    public override long GetNeededFeatures()
    {
        return AdminFEATURE.KeywordsSEO;
    }

    protected void InitSort()
    {
        DataTable dtSort = new BisSort().GetList();
        ddlSort.Items.Clear();
        ddlSort.Items.Add(new ListItem("所有分类", "0"));
        foreach (DataRow drSort in dtSort.Rows)
        {
            ddlSort.Items.Add(new ListItem(drSort["Name"].ToString(), drSort["ID"].ToString()));
        }
        dtSort.Dispose();

    }


    /// <summary>
    /// 列表绑定
    /// </summary>
    /// <param name="source"></param>
    /// <param name="e"></param>
    protected void rptKeywords_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        if (nAdminFeatures==1||SupportFeatures(AdminFEATURE.KeywordsList))
        {
            BisKeywords bisInfo = new BisKeywords();
            if (e.CommandName == "SetDiyLock")
            {
                string[] strItem = e.CommandArgument.ToString().Split(',');
                int nID = Convert.ToInt32(strItem[0]);
                bool bDiyLock = Convert.ToBoolean(strItem[1]);
                bisInfo.UpdateDiyLock(nID, !bDiyLock);
            }
            else if (e.CommandName == "SetSysLock")
            {
                string[] strItem = e.CommandArgument.ToString().Split(',');
                int nID = Convert.ToInt32(strItem[0]);
                bool bSysLock = Convert.ToBoolean(strItem[1]);
                bisInfo.UpdateSysLock(nID, !bSysLock);
            }
            RemoveKeywords();
            InitData();
        }
    }


    protected void BindMedical()
    {
        txtDateTime.Text = DateTime.Now.ToString("yyyy-MM-dd");
        BeginDateTime = txtDateTime.Text.Trim();
        DataTable dtMedical = new BisMedical().GetList();
        foreach (DataRow drMedical in dtMedical.Rows)
        {
            ddlMedical.Items.Add(new ListItem(drMedical["Name"].ToString(), (drMedical["ID"].ToString() + "," + drMedical["Status"].ToString())));
        }
        dtMedical.Dispose();
        InitBindMedical();
        
    }

    /// <summary>
    /// 页面初始化
    /// </summary>
    protected void InitData()
    {
        int nRecordCount = 0;
        int nPageCount = 0;

        if (string.IsNullOrEmpty(BeginDateTime))
        {
            BeginDateTime = DateTime.Now.ToString("yyyy-MM-dd");
        }
        DateTime dBeginTime = Convert.ToDateTime(BeginDateTime + " 00:00:00");
        //信息列表绑定
        DataTable dt = new BisKeywordsReport().GetReportList(MedicalID, SortID,Keywords, SysLock, DiyLock, CountIndex, dBeginTime, dBeginTime.AddDays(1), BeginCount, EndCount, anpPage.CurrentPageIndex, anpPage.PageSize, ref nPageCount, ref nRecordCount);
        nNum = (anpPage.CurrentPageIndex-1) * anpPage.PageSize;
        nNum++;
        //dt.Columns.Add("AllNewsCount", typeof(string));
        //dt.Columns.Add("AllLinkCount", typeof(int));
        //if (dt != null && dt.Rows.Count > 0)
        //{
        //    string strIDList = string.Empty;
        //    foreach (DataRow dr in dt.Rows)
        //    {
        //        strIDList += (string.IsNullOrEmpty(strIDList) ? "" : ",") + dr["ID"].ToString();
        //    }
        //    DataTable dtArticle = new BisArticleSEO().GetKeywordsArticleList(strIDList,true,dBeginTime, dBeginTime.AddDays(1));
        //    if (dtArticle != null && dtArticle.Rows.Count > 0)
        //    {
        //        foreach (DataRow drKeywords in dt.Rows)
        //        {
        //            DataRow[] drArticleList = dtArticle.Select("KID=" + drKeywords["ID"].ToString());
        //            int nAllNewsCount = 0;
        //            int nAllLinkCount = 0;
        //            foreach (DataRow drArticle in drArticleList)
        //            {
        //                switch (Convert.ToInt32(drArticle["UrlType"]))
        //                {
        //                    case 1:
        //                        nAllNewsCount++;
        //                        break;
        //                    case 2:
        //                        nAllLinkCount++;
        //                        break;
        //                }
        //            }
        //            drKeywords["AllNewsCount"] = nAllNewsCount;
        //            drKeywords["AllLinkCount"] = nAllLinkCount;
        //        }
        //    }
        //    dtArticle.Dispose();
        //}
        anpPage.RecordCount = nRecordCount;
        if (nRecordCount == 0)
        {
            noData.Style["display"] = "block";
        }
        else
        {
            noData.Style["display"] = "none";
        }
        rptKeywords.DataSource = dt;
        rptKeywords.DataBind();
        dt.Dispose();

        anpPage.CustomInfoHTML = string.Format("<div class=\"pagetag\">当前第<b>{0}</b>/<b>{1}</b>页 共<b>{2}</b>条记录</div>", new object[] { anpPage.CurrentPageIndex, anpPage.PageCount, anpPage.RecordCount });
    }

    /// <summary>
    /// 刷新页面
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnReset_Click(object sender, EventArgs e)
    {
        InitData();
    }

    /// <summary>
    /// 查询
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        
        SortID = Convert.ToInt32(ddlSort.SelectedValue);
        SysLock = Convert.ToInt32(ddlSysLock.SelectedValue);
        DiyLock = Convert.ToInt32(ddlDiyLock.SelectedValue);
        BeginDateTime = txtDateTime.Text.Trim();
        Keywords = txtKeywords.Text.Trim();
        try
        {
            BeginCount=Convert.ToInt32(txtBeginIndex.Text.Trim());
        }catch{
        }
         try
        {
            EndCount=Convert.ToInt32(txtEndIndex.Text.Trim());
        }catch{
        }
        InitBindMedical();
        anpPage.CurrentPageIndex = 1;
       // InitData();
    }

    /// <summary>
    /// 获取医院信息
    /// </summary>
    protected void InitBindMedical()
    {
        if (ddlMedical.Items.Count > 0)
        {
            BisSysRunLog bisSysRunLog = new BisSysRunLog();
            SysRunBeginDateTime = string.Empty;
            string[] tem = ddlMedical.SelectedValue.Split(',');
            MedicalID = Convert.ToInt32(tem[0]);
            Status = Convert.ToBoolean(tem[1]);
            if (Status)
            {
                //自家获取当天最后一次运行时间
                DataTable dtSysRunLog = bisSysRunLog.GetLastLog(Convert.ToDateTime(BeginDateTime), Convert.ToDateTime(BeginDateTime).AddDays(1));
                if (dtSysRunLog != null && dtSysRunLog.Rows.Count > 0)
                {
                    CountIndex = Convert.ToInt32(dtSysRunLog.Rows[0]["RunIndex"]);
                    SysRunBeginDateTime = dtSysRunLog.Rows[0]["BeginTime"].ToString();
                }
                else
                {
                    CountIndex = 1;
                }
                dtSysRunLog.Dispose();
            }
            else
            {
                //对手获取当天第一次运行时间
                DataTable dtSysRunLog = bisSysRunLog.GetLastLog(1,Convert.ToDateTime(BeginDateTime), Convert.ToDateTime(BeginDateTime).AddDays(1));
                if (dtSysRunLog != null && dtSysRunLog.Rows.Count > 0)
                {
                    SysRunBeginDateTime = dtSysRunLog.Rows[0]["BeginTime"].ToString();
                }
                CountIndex = 1;
                dtSysRunLog.Dispose();
            }
        }
    }

    /// <summary>
    /// 分页事件
    /// </summary>
    /// <param name="src"></param>
    /// <param name="e"></param>
    protected void anpPage_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e)
    {
        anpPage.CurrentPageIndex = e.NewPageIndex;
        InitData();
    }

    protected string ShowCss()
    {
        if (Status)
        {
            return "";
        }
        else
        {
            return "display:none;";
        }
    }
}